package com.iamteer.service.impl;

import com.iamteer.dao.IAdminDao;
import com.iamteer.dao.impl.AdminDao;
import com.iamteer.entity.Admin;
import com.iamteer.exception.UserExistsException;
import com.iamteer.service.IAdminService;

public class AdminService implements IAdminService {

	private IAdminDao adminDao = new AdminDao();
	
	@Override
	public void register(Admin admin) throws UserExistsException{
		try {
			// 1. 先根据用户名查询用户是否存在
			boolean flag = adminDao.userExists(admin.getUsername());

			// 2. 如果用户名存在，不允许注册
			if (flag) {
				// 不允许注册，给调用者提示
				throw new UserExistsException("用户名已经存在，注册失败");
			}
			// 3. 用户不存在，可以注册
			adminDao.save(admin);
		} catch (UserExistsException e) {
			throw e;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public Admin login(Admin admin) {
		
		return adminDao.findByNameAndPwd(admin);
	}

}
